package com.soxing.braindata.web.controller.research;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soxing.braindata.common.config.SystemConfig;
import com.soxing.braindata.dao.domain.request.ResearchSearchRequest;
import com.soxing.braindata.dao.domain.request.SearchRequest;
import com.soxing.braindata.dao.entity.*;
import com.soxing.braindata.dao.service.IResearchExtService;
import com.soxing.braindata.dao.service.IResearchService;
import com.soxing.braindata.dao.service.ISiteService;
import com.soxing.braindata.domain.request.SiteSearchRequest;
import com.soxing.braindata.dao.domain.vo.TinyResearch;
import com.soxing.braindata.domain.response.RStat;
import com.soxing.braindata.domain.response.SStat;
import com.soxing.braindata.domain.response.sync.SyncResponse;
import com.soxing.braindata.domain.rpc.OriginSubject;
import com.soxing.braindata.domain.vo.ResearchVo;
import com.soxing.braindata.service.manager.IResearchManager;
import com.soxing.braindata.service.manager.ISiteManager;
import com.soxing.braindata.service.sync.ISyncService;
import com.soxing.common.utils.response.MyPage;
import com.soxing.common.utils.response.WebResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author :durango
 * @date :2022/6/11 9:44
 * @description 课题控制器
 */
@Slf4j
@RestController
@RequestMapping("/research")
@Api(value = "课题管理", tags = {"课题管理"})
public class ResearchController {

    @Resource
    private IResearchManager researchManager;
    @Resource
    private ISiteManager siteManager;
    @Resource
    private ISyncService<OriginSubject> syncSubjectService;
    @Resource
    private IResearchService researchService;
    @Resource
    private ISiteService siteService;
    @Resource
    private IResearchExtService researchExtService;

    /**
     * 基于条件查询用户
     *
     * @param researchSearchRequest
     * @return
     */
    @PostMapping("/search")
    @ApiOperation(value = "基于条件查询课题")
    public WebResponse<MyPage<TinyResearch>> search(@RequestBody(required = false) ResearchSearchRequest researchSearchRequest) {
        if (researchSearchRequest == null) {
            researchSearchRequest = new ResearchSearchRequest();
        }
        return researchManager.search(researchSearchRequest);
    }

    @PostMapping("/searchAll")
    @ApiOperation(value = "查询所有课题")
    public WebResponse<MyPage<TinyResearch>> searchAll(@RequestBody(required = false) ResearchSearchRequest researchSearchRequest) {
        Page<Research> page = new Page<>(researchSearchRequest.getPageNum(), researchSearchRequest.getPageSize());
        Page<Research> result;
        result = researchService.page(page);
        return WebResponse.success(new MyPage<>(researchSearchRequest.getPageNum(), result.getPages(), result.getTotal(), result.getRecords()));
    }

    @PostMapping("/searchAllResearch")
    @ApiOperation(value = "不分页查询所有课题")
    public WebResponse<MyPage<TinyResearch>> searchAllResearch(@RequestBody(required = false) ResearchSearchRequest researchSearchRequest) {
        Page<Research> page = new Page<>(researchSearchRequest.getPageNum(), 10000);
        Page<Research> result;
        result = researchService.page(page);
        return WebResponse.success(new MyPage<>(researchSearchRequest.getPageNum(), result.getPages(), result.getTotal(), result.getRecords()));
    }

    @PostMapping("/searchSite")
    @ApiOperation(value = "基于条件查询中心")
    public WebResponse<MyPage<Site>> searchSite(@RequestBody(required = false) SiteSearchRequest siteSearchRequest) {
        if (siteSearchRequest == null) {
            siteSearchRequest = new SiteSearchRequest();
        }
        return siteManager.search(siteSearchRequest);
    }

    @PostMapping("/searchAllSite")
    @ApiOperation(value = "不分页查询所有课题中心")
    public WebResponse<MyPage<TinyResearch>> searchAllSite(@RequestBody(required = false) SiteSearchRequest siteSearchRequest) {
        Page<Site> page = new Page<>(siteSearchRequest.getPageNum(), 10000);
        Page<Site> result;
        result = siteService.page(page);
        return WebResponse.success(new MyPage<>(siteSearchRequest.getPageNum(), result.getPages(), result.getTotal(), result.getRecords()));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "基于id获取课题信息")
    public WebResponse<ResearchVo> getById(@PathVariable("id") Integer id) {
        return researchManager.getVoById(id);
    }

    @GetMapping("/getSiteBysitePkId")
    @ApiOperation(value = "基于中心编码获取中心信息")
    public WebResponse<Site> getSiteBysubjectPkId(@RequestParam String sitePkId) {
        return WebResponse.success(siteManager.getSiteBysitePkId(sitePkId));
    }

    @GetMapping("/getBysubjectPkId")
    @ApiOperation(value = "基于课题编码获取课题信息")
    public WebResponse<Research> getBysubjectPkId(@RequestParam String subjectPkId) {
        return researchManager.getBysubjectPkId(subjectPkId);
    }

    @PostMapping
    @ApiOperation(value = "新增课题")
    public WebResponse<Boolean> add(@RequestBody OriginSubject originSubject) {
        SyncResponse res = syncSubjectService.syncData(originSubject);
        if (res.getCode() == 0 ) {
            return WebResponse.success("添加成功");
        }else{
            return WebResponse.fail("添加失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "更新课题")
    public WebResponse<Boolean> update(Research research) {
        if (research.getSubjectPkId() == null) {
            return WebResponse.fail("课题编码为空");
        }
        return researchManager.update(research);
    }

    @PutMapping("/updateAdmin")
    @ApiOperation(value = "更新课题")
    public WebResponse<Boolean> updateAdmin(@RequestBody Research research) {
        if (research.getSubjectPkId() == null) {
            return WebResponse.fail("课题编码为空");
        }
        return researchManager.update(research);
    }


    @PostMapping("/delete/{id}")
    @ApiOperation(value = "基于id删除")
    public WebResponse<Boolean> deleteById(@PathVariable("id") Integer id) {
        Research r = researchService.getById(id);
        ResearchExt re = researchExtService.getOne(new LambdaQueryWrapper<ResearchExt>().eq(ResearchExt::getSubjectPkId, r.getSubjectPkId()));
        researchExtService.removeById(re.getId());
        return researchManager.deleteById(id);
    }


    @GetMapping("/getSiteStat")
    @ApiOperation(value = "获得分中心统计情况")
    WebResponse<SStat> getSiteStat(@RequestParam String subjectPkId, @RequestParam String sitePkId){
        return siteManager.getSiteStat(subjectPkId,sitePkId);

    }


    @GetMapping("/getResearchStatByUser")
    @ApiOperation(value = "根据用户获得其所参与的所有队列统计情况")
    WebResponse<RStat> getResearchStatByUser(@RequestParam String userName){
        return researchManager.getResearchStatByUser(userName);
    }

    @GetMapping("/getResearchStat")
    @ApiOperation(value = "获得课题统计情况")
    WebResponse<RStat> getSResearchStat(@RequestParam String subjectPkId){
        return researchManager.getResearchStat(subjectPkId);
    }

    @GetMapping("/getByResearchByUser")
    @ApiOperation(value = "获取用户所参与的课题")
    public WebResponse<MyPage<Research>> getByResearchByUser(@RequestParam String userName,@RequestParam int pageSize,@RequestParam int pageNum) {
        return researchManager.getByResearchByUser(userName,pageSize,pageNum);
    }



}
